<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateWaimaiCallbackLogsSplitTables extends Migration
{
    private $name;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->id();
            $table->unsignedTinyInteger('sourceType')->default(0)->comment('订单来源 3美团 4饿了么');
            $table->unsignedBigInteger('orderId')->default(0);
            $table->unsignedInteger('shopId')->default(0);
            $table->string('thirdShopId', 64)->default('')->comment('外卖平台方的店铺id');
            $table->string('route', 128)->default('')->comment('回调地址');
            $table->unsignedTinyInteger('callType')->default(0);
            $table->text('requestData');
            $table->string('responseData', 255)->default('');
            $table->string('errMsg', 32)->default('');
            $table->integer("createdAt")->default(0)->comment("创建时间");

            $table->index('orderId', 'idx_orderId');
            $table->index('thirdShopId', 'idx_thirdShopId');
            $table->index('shopId', 'idx_shopId');
            $table->index('callType', 'idx_callType');
            $table->collation = 'utf8mb4_general_ci';
            $table->comment = '外卖平台的接口回调日志'. $this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->create('waimai_callback_logs' . $this->name, $cFunc);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->dropIfExists('waimai_callback_logs' . $this->name);
        }
    }
}
